package connection

import (
	"sync"
)

type Map interface {
	Store(key any, value any)
	Load(key any) (any, bool)
	Delete(key any)
	List() map[any]any
	Len() int
}

type syncMap struct {
	mx    sync.Mutex
	dirty map[any]any
}

func NewMap() Map {
	return &syncMap{
		dirty: make(map[any]any),
	}
}

func (m *syncMap) Store(key any, value any) {
	m.mx.Lock()
	defer m.mx.Unlock()
	m.dirty[key] = value
	//fmt.Println("增加:", key, "现在有长度：", len(m.dirty))
}

func (m *syncMap) Load(key any) (any, bool) {
	//m.mx.Lock()
	//defer m.mx.Unlock()
	value, ok := m.dirty[key]
	//fmt.Println("长度", len(m.dirty))
	return value, ok
}

func (m *syncMap) Delete(key any) {
	m.mx.Lock()
	defer m.mx.Unlock()
	delete(m.dirty, key)
	//fmt.Println("删除:", key, "现在有长度：", len(m.dirty))
}

func (m *syncMap) List() map[any]any {
	return m.dirty
}

func (m *syncMap) Len() int {
	return len(m.dirty)
}
